﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;

namespace SignalRDemo.Common.Extensions
{
    /// <summary>
    /// Common extensions of <see cref="T:System.Object" />.
    /// </summary>
    public static class TypeExtensions
    {
        /// <summary>Gets schema of the specified object.</summary>
        /// <param name="type">The entity type.</param>
        /// <returns>A datatable of specified object with no records.</returns>
        public static DataTable GetSchema(this Type type)
        {
            if (type == (Type)null)
                throw new ArgumentNullException(nameof(type));
            DataTable dataTable = new DataTable(type.Name);
            foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(type))
            {
                Type type1 = Nullable.GetUnderlyingType(property.PropertyType);
                if ((object)type1 == null)
                    type1 = property.PropertyType;
                Type type2 = type1;
                dataTable.Columns.Add(property.Name, type2);
            }
            return dataTable;
        }
    }
}